/**
 * Created by shenhai on 15/9/1.
 */

var async = require('async');
var mysqlUtil = require('../../../common/utils/mysqlUtil');





exports.menuAllListService = function (req, callback) {

    var sql = "select * from sys_menu where isValid = 1";
    mysqlUtil.query(sql, callback);
};


exports.menuListService = function (req, callback) {

    var body = req.body;
    var page = req.params.page;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;
    var totalPage = 0;


    var sql1 = "select count(*) as count from sys_menu where 1 = 1";
    var sql2 = "select * from sys_menu where 1 = 1 order by parentID,menuOrder limit " + index +"," + size;

    if (body.menuName) {
        sql1 = sql1 + " and menuName like '%" + body.menuName + "%'";
        sql2 = "select * from sys_menu where 1 = 1 and menuName like '%" + body.menuName +"%' order by  parentID,menuOrder limit " + index +"," + size;
    }


    async.series([

        function (callbackInner) {
            mysqlUtil.query(sql1, function (err, result) {

                if (err || result.length == 0) {
                    callbackInner(err, null);
                }else {
                    var count = result[0].count;
                    totalPage = Math.ceil(count/size);
                    var temp = {count: count, totalPage:totalPage};


                    if (page > totalPage) {
                        page = totalPage;
                        index = (Number(page) - 1) * size;
                        sql2 = "select * from sys_menu where 1 = 1 order by parentID,menuOrder limit " + index +"," + size;
                    }

                    logger.debug(temp);
                    callbackInner(err, temp);
                }

            });
        },

        function (callbackInner) {
            mysqlUtil.query(sql2, function (err, result) {

                result.forEach(function(temp) {
                    temp.isValid = temp.isValid.readUInt8(0,[true]);
                });

                callbackInner(err, result);
            });
        }

    ], callback);
};


exports.menuAddService = function (req, callback) {

    var body = req.body;
    if(!body.menuClass){
        body.menuClass = "am-icon-table";
    };
    
    body.isValid = 1;


    var sql = "insert into sys_menu set ?";

    mysqlUtil.query(sql, [body],function (err, result) {
        callback(err, result);
    });

};


exports.menuDeleteService = function (req, callback) {

    var body = req.body;
    if (!body.menuID) {
        callback({domain:"没有sys_menuID"}, null);
    }

    logger.debug(body.menuID);

    var temp = "(";
    for(var i = 0, leng = body.menuID.length;i<leng;i++) {
        if (i == leng - 1) temp = temp + body.menuID[i];
        else temp = temp + body.menuID[i] +",";
    }
    temp += ")";

    var sql = "delete from sys_menu where sys_menuID in " + temp;
    mysqlUtil.query(sql, callback);
};




exports.menuEditService = function (req, callback) {

    var body = req.body;


    logger.debug(body);

    if (!body.menuID) {
        callback("没有menuID", null);
    }else {
        body.menuID = Number(body.menuID);
        var sql = "update sys_menu set ? where sys_menuID = ?";
        mysqlUtil.query(sql, [body.menu, body.menuID],function (err, result) {
            callback(err, result);
        });
    }


};


exports.allMenuListService = function (req, callback) {

    var body = req.body;

    var sql = "select * from sys_menu where isValid = 1 order by parentID,menuOrder";
    mysqlUtil.query(sql, function (err, result) {
        callback(err, result);
    });
};


